module.exports = function(id,Args,onload){
  Args = Args || {};
  var _this = this;
  var devicePixelRatio = Args.devicePixelRatio;
  this.canvas = null;
  this.ctx = null;
  if(/WeChatMiniApp/.test(Args.platform)){
    if(!devicePixelRatio) devicePixelRatio = wx.getSystemInfoSync().devicePixelRatio;
    if(onload){
      var query = wx.createSelectorQuery()
      query.select('#' + id).fields({ node: true, size: true }).exec(ret => {
        this.canvas = ret[0].node;
        this.canvas.width = ret[0].width * devicePixelRatio;
        this.canvas.height = ret[0].height * devicePixelRatio;
        this.ctx = this.canvas.getContext('2d');
        onload(this.ctx,this.canvas);
      })
    }else{
      this.ctx = wx.createCanvasContext(id);
    }
  }else{
    if(!devicePixelRatio) devicePixelRatio = window.devicePixelRatio;
    this.canvas = document.getElementById(id);
    this.canvas.width = this.canvas.width * devicePixelRatio;
    this.canvas.height = this.canvas.height * devicePixelRatio;
    var rect = this.canvas.getBoundingClientRect();
    canvas.style.width = rect.width + 'px';
    canvas.style.height = rect.height + 'px';
    this.ctx = this.canvas.getContext(Args.type || '2d');
  }
  this.fillRect = function(x,y,width,height){
    this.ctx.fillRect(x * devicePixelRatio,y * devicePixelRatio,width * devicePixelRatio,height * devicePixelRatio);
  }
  this.Image = function(url,style){
    style = style || {};
    for(var key in style) if(style[key] > 0) style[key] *= devicePixelRatio;
    if(/WeChatMiniApp/.test(Args.platform) && !onload){
      wx.downloadFile({
        url:url,
        success:function(ret){
          _this.ctx.drawImage(ret.tempFilePath, style.left || 0, style.top || 0, style.width, style.height);
          if(typeof style.success == 'function') style.success();
        },
        fail:style.fail
      })
    }else{
      var image = onload ? this.canvas.createImage() : new Image();
      if(image.setAttribute) image.setAttribute('crossOrigin','anonymous');
      image.onload = function(){
        //_this.ctx.drawImage(image,style.sx || style.left || 0,style.sy || style.top || 0,style.switdh || style.width, style.sheight || style.height, style.left || 0, style.top || 0, style.width, style.height);
        _this.ctx.drawImage(image,style.left || 0, style.top || 0, style.width, style.height);
        if(typeof style.success == 'function') style.success();
      }
      image.onerror = style.fail;
      image.onabort = style.fail;
      image.src = url;
    }
  }
  this.Text = function(content,style){
    style = style || {};
    for(var key in style) if(style[key] > 0) style[key] *= devicePixelRatio;
    style.color = style.color || '#000000';
    style.fontSize = style.fontSize || 12;
    if(/WeChatMiniApp/.test(Args.platform) && !onload){
      this.ctx.setFillStyle(style.color);
      this.ctx.setFontSize(style.fontSize);
      //ctx.setTextBaseline('alphabetic');
    }else{
      this.ctx.fillStyle = style.color;
      this.ctx.font = (style.fontWeight || '') + ' ' + style.fontSize + 'px ' + (style.fontFamily || 'auto');
      //ctx.textBaseline = 'top'
    }
    function drawText(content,style){
      var rect = {
        width:style.width || 0,
        height:style.height || 0,
        top:style.top || 0,
        left:style.left || 0,
        line:content ? 1 : 0
      };
      var lineHeight = style.lineHeight || (style.fontSize * 1.5)
      var contents = content.split('\n');
      if(contents.length == 1){
        var x = style.left || 0,letterSpacing = style.letterSpacing || 0, y = (style.top || 0) + (lineHeight - style.fontSize) / 2,width = 0;
        for(var i = 0; i < content.length; i ++){
          var now = content.charAt(i);
          if(style.width){
            if(width + letterSpacing > style.width){
              if(rect.height && (rect.line + 1) * lineHeight > rect.height) break;
              rect.line ++;
              _this.ctx.fillText(now, x,rect.line * lineHeight + y);
              width = _this.ctx.measureText(now).width;
            }else{
              _this.ctx.fillText(now, x + width + letterSpacing,rect.line * lineHeight + y);
              width += _this.ctx.measureText(now).width + letterSpacing;
            }
            if(i == content.length - 1){
              rect.height = rect.line * lineHeight;
            }
          }else{
            _this.ctx.fillText(now, x + width + letterSpacing,rect.line * lineHeight + y);
            width += _this.ctx.measureText(now).width + letterSpacing;
            if(i == content.length - 1) {
              rect.width = width;
              rect.height = lineHeight;
            }
          }
        }
      }else{
        var y = 0,width = 0,height = 0,line = 0;
        for(var i = 0; i < contents.length; i ++){
          style.top += y;
          if(rect.height && height + lineHeight > rect.height) break;
          var _rect = drawText(contents[i],style);
          y = _rect.height;
          if(_rect.width > width) width = _rect.width;
          height += _rect.height;
          line += _rect.line;
        }
        if(!rect.width) rect.width = width;
        if(!rect.height) rect.height = height;
        rect.line = line;
      }
      rect.width /= devicePixelRatio;
      rect.height /= devicePixelRatio;
      return rect;
    }
    return drawText(content,style);
  }
  this.toDataURL = function(args){
    args = args || {};
    if(/WeChatMiniApp/.test(Args.platform) && !onload){
      this.ctx.draw(false,function(){
        setTimeout(function(){
          wx.canvasToTempFilePath({
            canvasId: id,
            fileType: args.fileType,
            quality: args.quality,
            success:function(ret) {
              if(typeof args.success == 'function') args.success(ret.tempFilePath);
            },
            fail:args.fail
          })
        },args.times || 200)
      })
    }else{
      if(typeof args.success == 'function') args.success(this.canvas.toDataURL('image/' + args.fileType,args.quality));
    }
  }
}